#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDoubleValidator>
#include <QRegExp>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    udpSocket = new QUdpSocket(this);
    connect(udpSocket, &QUdpSocket::readyRead, this, &MainWindow::readMsg);//接收数据
    connect(udpSocket, &QUdpSocket::disconnected, this, &MainWindow::on_internet_disconnect_clicked);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_internet_connect_clicked()
{
    host = ui->ip_server->text().trimmed();
    port = ui->ip_port->text().toInt();
    local_port=ui->ip_port_2->text().toInt();
    host = ui->ip_server->text();
    if(udpSocket->state() == QAbstractSocket::UnconnectedState){
        udpSocket->bind(QHostAddress::Any, local_port);
        ui->internet_connect->setText("重连");
    }
        local_ip = QHostAddress(ui->ip_local->text().trimmed());
        if(host.isEmpty() || port == 0)
        {
            QMessageBox::information(NULL, "警告", "请输入完整信息", QMessageBox::Yes);
            return;
        qDebug()<<host<<","<<port;
    }
}

void MainWindow::on_internet_disconnect_clicked()
{
    udpSocket->abort();
    ui->internet_connect->setText("连接");
    qDebug()<<"Disconnected";
}

//请求反馈数据（设定下为系统周期上传）
void MainWindow::readMsg()
{
    char buf[10240] = {0};//接收数据
    qint64 len = udpSocket->readDatagram(buf, sizeof(buf), &local_ip, &local_port);
    QByteArray rawData = QByteArray::fromRawData(buf, len);
    qDebug()<<"接收数据"<<rawData;
    ui->rev->setText(rawData);

        //判断字头和字尾
//    if(receive_from_plc.head!=0x66666666||receive_from_plc.tail!=0x88888888)
//    {
//        return;
//    }
//        qDebug()<<"receive:"<<data;
//    qDebug()<<"接收数据中";
    if(len < 0){
        qDebug()<<"接收数据失败！";
        return;
    }
    QString head = rawData.left(4);
    QString count = rawData.mid(4,5);
    QString xyr = rawData.mid(9).trimmed();


    QStringList data = xyr.split(QRegExp("[xyr/\x20/]"));

    //如果发送数据不全就忽略
    if(data.length() < 4)
    {
        return;
    }
    QString x,y,r;
    x = data[1];
    y = data[2];
    r = data[3];
    if(x.isEmpty())
        x = QString("0");
    if(y.isEmpty())
        y = QString("0");
    if(r.isEmpty())
        r = QString("0");
    SEND_TO_PLC send;
    send.x=x.toFloat();
    send.y=y.toFloat();
    send.r=r.toFloat();
    ui->x->display(send.x);
    ui->y->display(send.y);
    ui->r->display(send.r);
    QByteArray sendData=serializeFrame(send);//数据帧
    udpSocket->writeDatagram(sendData, QHostAddress(host), port);
    qDebug()<<"Send Data:"<<data;

}

QByteArray MainWindow::serializeFrame(SEND_TO_PLC frame)
{
    QByteArray data;
    QDataStream stream(&data, QIODevice::WriteOnly);
//    QDataStream::setFloatingPointPrecision(QDataStream::SinglePrecision);

    stream.setByteOrder(QDataStream::BigEndian);
    stream<<frame.head<<frame.count;
    //QDataStream默认浮点型数据为8字节
    writeReal(stream,frame.x);
    writeReal(stream,frame.y);
    writeReal(stream,frame.r);
    //stream<<frame.tail;
            //frame.max_v<<frame.max_omega<<frame.max_a<<frame.max_beta<<
    return data;
}

void MainWindow::writeReal(QDataStream& out, float f)
{
    out.setFloatingPointPrecision(QDataStream::SinglePrecision);
    out << f;
}
